<?php
function person($n){
	for($i=0; $i<$n; $i++){
		echo "<img src = 'html/assets/img/icon-people.png'>";
	}
}

if ( !function_exists('close') ) :
function close(){
  echo "<html><body onLoad=\"javascript:self.close();\">";
  echo "</body></html>";
}
endif;

if ( !function_exists('close_and_refresh') ) :
function close_and_refresh(){
   echo "<html><body onLoad=\"javascript:self.close();window.opener.location.reload();\">";
   echo "</body></html>";
}
endif;

if ( !function_exists('alert') ) :
function alert($msg) {
  echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>';
  echo "<script language = 'javascript'>";
  echo "alert('$msg')";
  echo "</script>";
}
endif;

if ( !function_exists('top_redirect') ) :
function top_redirect($url) {
  echo "<script language = 'javascript'>";
  echo "window.top.location.href='$url';";
  echo "</script>";
}
endif;

if ( !function_exists('redirect') ) :
/**
 * Redirects to another page.
 */
function redirect($url, $d = 0, $msg = "") {

    echo "<html><head>\n";
    echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>\n";
    echo "<meta http-equiv='refresh' content='$d; URL=$url'>\n";
    echo "</head><body>$msg</body></html>\n";
    exit;
}
endif;

if ( !function_exists('_php') ) :
/**
 * Get Templete file name ex. index.html will get index.php
 */
function _php($path) {
	$path_parts = pathinfo($path);
	return $path_parts['dirname'] ."/". $path_parts['filename'] . ".php";
}
endif;

if ( !function_exists('_html') ) :
/**
 * Get Templete file name ex. index.php will get index.html
 */
function _html($path) {
	$path_parts = pathinfo($path);
	return $path_parts['dirname'] ."/". $path_parts['filename'] . ".html";
}
endif;

function now(){
	return date("Y-m-d H:i:s");
}

function topic_date_format($date){
	return 	date("F d, Y", strtotime($date));
}

function comment_date_format($date){
	return 	date("F d, Y H:i:s", strtotime($date));
}

function pagenavi($num, $p, $list_per_page = 12, $arg = ""){

	global $item_start, $item_end, $cid;

    if($p == null) $p = 1;
    elseif($p < 1) $p = 1;
    elseif($p > ceil($num / $list_per_page))
        $p = ceil($num / $list_per_page);

    $item_start = ($p-1) * $list_per_page;
    $item_end = $start + $list_per_page;

	$pagenavi_html = "<div class='story-page'>\n";
	$pagenavi_html .= "<ul class='page-list'>\n";
	for($i = 1; $i <= ceil($num / $list_per_page); $i++){
		if($i == $p){
			$pagenavi_html .= "<li><a href = '?p=$i&$arg' class = 'active'>".$i."</a></li>\n";
		}else{
			$pagenavi_html .= "<li><a href = '?p=$i&$arg'>".$i."</a></li>\n";
		}
	}
	$pagenavi_html .= "</ul></div>\n";

	return $pagenavi_html;
}

function strip($text) {
	$text = str_replace(']]>', ']]&gt;', $text);
	$text = strip_tags($text);

	return $text;
}

function excerpt($text, $excerpt_length = 0) {
	
	if($excerpt_length == 0){

		$tmp = explode('<!-- pagebreak -->', $text);
		$text = $tmp[0];
		$text = str_replace(']]>', ']]&gt;', $text);
		$text = strip_tags($text);

		return $text;
	}

	$text = str_replace(']]>', ']]&gt;', $text);
	$text = strip_tags($text);

	$excerpt_more = ' ...';

	$words = explode(' ', $text);

    $result = "";
    $_result = "";

    foreach($words as $k => $v){
        $result .= $v." ";

        if( strlen($result)/3 > $excerpt_length ){
            if(strlen($result)/3 > $excerpt_length + 15) {
                return $_result . $excerpt_more;
            }else{
                return $result . $excerpt_more;
            }
        }

        $_result .= $v." ";
    }

	return $result;
}

/*
function excerpt_eng($text, $excerpt_length) {
		
	$text = str_replace(']]>', ']]&gt;', $text);
	$text = strip_tags($text);

	$excerpt_more = ' ...';
	
	$words = explode(' ', $text, $excerpt_length + 1);
	if (count($words) > $excerpt_length) {
		array_pop($words);
		$text = implode(' ', $words);
		$text = $text . $excerpt_more;
	}
	return $text;
}
*/

if ( !function_exists('get_thumb_name') ) :

function get_thumb_name($thumb, $prefix = "-th"){
  $path_parts = pathinfo($thumb);

  $f = $path_parts['dirname']."/".$path_parts['filename'] . $prefix . "." .$path_parts['extension'];
  return $f;
}

endif;


if ( !function_exists('get_pic_name') ) :

function get_pic_name($pic){
  $path_parts = pathinfo($pic);

  $tmp = explode("-", $path_parts['filename']);
  $prefix = end($tmp);

  $path_parts['filename'] = str_replace("-".$prefix,"",$path_parts['filename']);

  $f = $path_parts['dirname']."/".$path_parts['filename'] . "." .$path_parts['extension'];
  return $f;
}

endif;


if ( !function_exists('upload') ) :

function upload($file, $savefile = ""){

    if ($_FILES[$file]['name'] != ''){

        if ($_FILES[$file]['error'] > 0) {
            echo 'Problem: ';
            switch ($_FILES[$file]['error']){
                case 1:
                    echo 'File exceeded upload_max_filesize';
                    break;
                case 2:
                    echo 'File exceeded max_file_size';
                    echo 'please resize picture to less than 600K then try again';
                    break;
                case 3:
                    echo 'File only partially uploaded';
                    break;
                case 4:
                    echo 'No file uploaded';
                    break;
            }
            return false;
        }

        if (is_uploaded_file($_FILES[$file]['tmp_name'])){

            if($savefile == ""){
                $path_info = pathinfo($_FILES[$file]['name']);
                $filename = time();
                $savefile = UPLOAD. $filename .".".$path_info['extension'];
                $destination = URL."upload/".$filename .".".$path_info['extension'];

                if(file_exists($savefile)){
                    $filename = time()+1;
                    $savefile = UPLOAD. $filename .".".$path_info['extension'];
                    $destination = URL."upload/". $filename .".".$path_info['extension'];
                }
            }

            if (!move_uploaded_file($_FILES[$file]['tmp_name'], $savefile)){
                echo 'Problem: Could not move file to destination directory';
                return false;
            }else{

                return array($destination, $savefile);
            }
        }else{
            echo 'Problem: Possible file upload attack. Filename: '.$file;
            return false;
        }
        chmod($savefile, 0755);
    }

    return false;
}

endif;

if ( !function_exists('nivoslider') ) :
function nivoslider(){
	echo '<link rel="stylesheet" href="'.URL.'script/nivo-slider/nivo-slider.css" type="text/css"/>'."\n";
	echo '<script src="'.URL.'script/nivo-slider/jquery.nivo.slider.js" type="text/javascript"></script>'."\n";
}
endif;

if ( !function_exists('scroll_to') ) :
function scroll_to(){
	echo '<script src="'.URL.'script/jquery.scrollTo-min.js" type="text/javascript"></script>'."\n";
}
endif;

if ( !function_exists('tiny_mce') ) :
function tiny_mce($init = 'init.js'){
	echo '<script src="'.URL.'script/tinymce/jscripts/tiny_mce/tiny_mce.js" type="text/javascript"></script>'."\n";
	echo '<script src="'.URL.'script/tinymce/'.$init.'" type="text/javascript"></script>'."\n";
}
endif;

function hover_intent() {
  echo '<script type="text/javascript" src="'.URL.'script/jquery.hoverIntent.js"></script>';
}

if ( !function_exists('jquery') ) :
function jquery(){
	echo "<script type='text/javascript' src='".URL."script/jquery-latest.min.js'></script>"."\n";
}
endif;

if ( !function_exists('jquery_validate') ) :
function jquery_validate($lan = 'en'){
	echo '<script src="'.URL.'script/validate/jquery.validationEngine-'.$lan.'.js" type="text/javascript" charset="utf-8"></script>'."\n";
	echo '<script src="'.URL.'script/validate/jquery.validationEngine.js" type="text/javascript" charset="utf-8"></script>'."\n";
	echo '<link rel="stylesheet" href="'.URL.'script/validate/jquery.validate.css" type="text/css"/>'."\n";
}
endif;

if ( !function_exists('fancybox') ) :
function fancybox(){
	echo '<script src="'.URL.'script/fancybox/jquery.fancybox-1.3.4.pack.js" type="text/javascript"></script>'."\n";
	echo '<script src="'.URL.'script/fancybox/jquery.easing-1.3.pack.js" type="text/javascript"></script>'."\n";
	echo '<script src="'.URL.'script/fancybox/jquery.mousewheel-3.0.4.pack.js" type="text/javascript"></script>'."\n";
	echo '<link href="'.URL.'script/fancybox/jquery.fancybox-1.3.4.css" rel="stylesheet" type="text/css">'."\n";
}
endif;

if ( !function_exists('jquery_ui') ) :
function jquery_ui($theme = "ui-lightness"){
	echo '<script src="'.URL.'script/jquery-ui/jquery-ui-custom.min.js" type="text/javascript"></script>'."\n";
	echo '<link href="'.URL.'script/jquery-ui/'.$theme.'/theme.css" rel="stylesheet" type="text/css">'."\n";
}
endif;

if ( !function_exists('jquery_rating') ) :
function jquery_rating(){
	echo '<link rel="stylesheet" href="'.URL.'script/rating/jRating.jquery.css" type="text/css" />'."\n";
	echo '<script type="text/javascript" src="'.URL.'script/rating/jRating.jquery.js"></script>'."\n";
}
endif;

if ( !function_exists('jquery_easing') ) :
function jquery_easing(){
	echo '<script src="'.URL.'script/jquery.easing.1.3.js" type="text/javascript"></script>'."\n";
}
endif;

if ( !function_exists('html5') ) :
function html5(){
	echo "<!--[if lt IE 9]>"."\n";
	echo "<script type='text/javascript' src='".URL."script/html5.js'></script>"."\n";
	echo "<![endif]-->"."\n";
}
endif;

if ( !function_exists('cufon') ) :
function cufon($font){
	echo '<script src="'.URL.'script/cufon/cufon-yui.js" type="text/javascript"></script>'."\n";
	echo '<script src="'.URL.'font/'.$font.'.font.js" type="text/javascript"></script>'."\n";
}
endif;

if ( !function_exists('highslide') ) :
function highslide(){
	echo '<script type="text/javascript" src="'.URL.'script/highslide/highslide-full.js"></script>'."\n";
	echo '<link rel="stylesheet" type="text/css" href="'.URL.'script/highslide/highslide.css" />'."\n";
	echo '<script type="text/javascript">'."\n";
	echo 'hs.graphicsDir = "'.URL.'script/highslide/graphics/";'."\n";
	echo '</script>';
}
endif;

if ( !function_exists('uploadify') ) :
function load_js($file = "function.js"){
	echo '<script type="text/javascript" src="'.URL.'script/'.$file.'"></script>'."\n";
}
endif;

if ( !function_exists('swfupload') ) :
function swfupload(){
	echo '<script type="text/javascript" src="'.URL.'script/swfupload/swfupload/swfupload.js"></script>';
	echo '<script type="text/javascript" src="'.URL.'script/swfupload/js/handlers.js"></script>';
}
endif;

if ( !function_exists('uploadify') ) :
function uploadify(){
	echo '<link href="'.URL.'script/uploadify/uploadify.css" type="text/css" rel="stylesheet" />'."\n";
	echo '<script type="text/javascript" src="'.URL.'script/uploadify/jquery.uploadify-3.1.min.js"></script>'."\n";
}
endif;

if ( !function_exists('multi_upload') ) :
function multi_upload($id, $data = "", $script = ""){
?>

<link href="<?=URL?>script/uploadify/uploadify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="<?=URL?>script/uploadify/jquery.uploadify-3.1.min.js"></script>
<script language = "javascript">
	$(document).ready(function() {
<?php
	if($script == ""){
		$script = URL."script/uploadify/uploadify.php";
	}

	echo "var script = '$script';\n";

	if($data == ""){
		$data = "{'folder' : '/upload/gallery/'}";
	}

	echo "var data = $data;\n";

?>
        $("#<?=$id?>").uploadify({
            'swf'      : "<?=URL?>script/uploadify/uploadify.swf",
            'uploader' : script,
			'fileTypeDesc' : 'Image Files',
            'fileTypeExts' : '*.gif; *.jpg; *.png',
			'multi'     : true,
			'width'		: 90,
			'height'	: 20,
			'formData'  : data,
			'onQueueComplete' : function(queueData) {
				//alert(queueData.uploadsSuccessful + ' files were successfully uploaded.');
				window.location.reload();
			},
        });
    }); 
</script>
<?php
}

endif;
?>